﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Repository;
using UI.Models;
using UI.Helper;

namespace UI.Controllers
{
    public class ProductController : Controller
    {
        //
        // GET: /Product/
        const int HOWMANY = 9;

        public ActionResult Index()
        {
            return View();
        }

        #region action public ActionResult Detail(int id)
        public ActionResult Detail(int id)
        {
            var repository = RepositoryManager.CreateProductRepository();
            var repositoryImg = RepositoryManager.CreateImageRepository();
            var repositoryCate = RepositoryManager.CreateCategoryRepository();

            var objProduct = repository.GetById(id);

            if (objProduct != null)
            {
                var model = new ProductDetailModels()
                {
                    liImages = repositoryImg.GetByProductId(id),
                    Image = repositoryImg.GetImageByProductID(id),
                    ProductId = objProduct.Id,
                    Description = objProduct.Description,
                    IsHot = objProduct.IsHot,
                    Name = objProduct.Name,
                    Priority = objProduct.Priority,
                    CategoryName = objProduct.Category.Name,
                    Address = objProduct.Address
                };
                return View(model);
            }

            return View();
        }
        #endregion

        #region action public ActionResult ListCategory()
        public ActionResult ListCategory()
        {
            var ListCategoryModel = new ListCategoryModel();
            List<CategoryProducts> liCate = new List<CategoryProducts>();
            var repository = RepositoryManager.CreateProductRepository();
            var repositoryImg = RepositoryManager.CreateImageRepository();
            var repositoryCate = RepositoryManager.CreateCategoryRepository();

            var ListCategory = repositoryCate.GetAll();
            foreach (var tmp in ListCategory)
            {
                CategoryProducts obj = new CategoryProducts();
                obj.CategoryId = tmp.Id;
                obj.Image = tmp.ImageUrl;
                obj.Name = tmp.Name;
                obj.Products = repository.GetProductByCateID(obj.CategoryId, 10);
                liCate.Add(obj);
            }
            ListCategoryModel.liCategoryProducts = liCate;
            return View(ListCategoryModel);
        }
        #endregion

        #region public ActionResult ListProductByCategory(int id)
        public ActionResult ListProductByCategory(int id, int? page)
        {
            if (page == null)
                page = 1;

            var rep = RepositoryManager.CreateProductRepository();
            var repCate = RepositoryManager.CreateCategoryRepository();

            var model = rep.GetProductByCateID(id, HOWMANY, (int)page);

            var productModel = new ProductListModel();

            var Cate = repCate.GetById(id);
            productModel.NameCategory = Cate.Name;
            productModel.ProductDetails = new List<ProductDetailModels>();

            foreach (var tmp in model)
            {
                var productDetail = new ProductDetailModels()
                {
                    ProductId = tmp.Id,
                    Name = tmp.Name,
                    Description = Utils.Substring(tmp.Description, 1000),
                    CategoryId = tmp.Category.Id,
                    liImages = tmp.Images.ToList(),
                    CategoryName = tmp.Category.Name
                };

                productModel.ProductDetails.Add(productDetail);
            }

            var totalPage = rep.GetTotalPageByCategory(id, HOWMANY);

            ViewData[Constants.TOTAL_PAGE] = totalPage;

            ViewData[Constants.PAGE_INDEX] = page;

            return View(productModel);
        }
        #endregion

        #region public ActionResult Search(int iprovinceID, int idistrict, int? page)
        public ActionResult Search(int province, int district, int? page)
        {
            if (page == null)
                page = 1;

            var rep = RepositoryManager.CreateProductRepository();

            var model = rep.GetProductSearch(province, district, HOWMANY, (int)page);

            var productModel = new SearchProductModel();

            //Get Name Search
            string Name = string.Empty;
            var repPro = RepositoryManager.CreateProvinceRepository();
            var repDis = RepositoryManager.CreateDistrictRepository();
            if (district == 0)
            {
                var objPro = repPro.GetById(province);
                Name = objPro.Name;
            }
            else
            {
                var objPro = repPro.GetById(province);
                Name = objPro.Name;
                var objDis = repDis.GetById(district);
                Name += ", ";
                Name += objDis.Name;
            }
            productModel.NameSearch = Name;
            productModel.provinceID = province;
            productModel.districtID = district;

            productModel.ProductDetails = new List<ProductDetailModels>();

            foreach (var tmp in model)
            {
                var productDetail = new ProductDetailModels()
                {
                    ProductId = tmp.Id,
                    Name = tmp.Name,
                    Description = Utils.Substring(tmp.Description, 1000),
                    CategoryId = tmp.Category.Id,
                    liImages = tmp.Images.ToList(),
                    CategoryName = tmp.Category.Name
                };

                productModel.ProductDetails.Add(productDetail);
            }

            var totalPage = rep.GetTotalPageSearch(province, district, HOWMANY);

            ViewData[Constants.TOTAL_PAGE] = totalPage;

            ViewData[Constants.PAGE_INDEX] = page;

            return View(productModel);
        }
        #endregion
    }
}
